package com.lhl.shiro.shiroconfig;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author LHL
 * @date 2018/1/8
 */


public class StatelessRealm extends AuthorizingRealm {

    private static final Logger LOGGER = LoggerFactory.getLogger(StatelessRealm.class);

    @Override
    public boolean supports(AuthenticationToken token) {
        //仅支持StatelessToken类型的Token
        return token instanceof StatelessToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //根据用户名查找角色，请根据需求实现

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {

        String userId = token.getPrincipal().toString();
        LOGGER.info("[StatelessRealm.doGetAuthenticationInfo] userId={}", userId);
        return new SimpleAuthenticationInfo(userId, token.getCredentials(), getName());
    }
}
